<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\pt\controller;

use think\admin\Controller;
use think\facade\Db;

/**
 * Class Group
 * @package app\group\controller
 */
class Group extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'PtGroup';

    /**
     * 拼团活动列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '参团列表';
        $this->_query($this->table)->dateBetween('add_time')->order('update_time desc')->page();
    }


    /**
     * 拼团添加
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function groupAdd()
    {
        $this->_applyFormToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param $vo
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            #科目列表
            $menu_list = Db::table('pt_menu')->where(['status' => 1])->select()->toArray();
            $new_menu_list = $this->arr2tree($menu_list);
            $vo['menu_list'] = $new_menu_list;
            if (empty($vo['price_list'])) $vo['price_list'] = json_encode([]);
            $vo['menu_keys'] = !empty($vo['menu_keys']) ? explode(',', $vo['menu_keys']) : [];
        } elseif ($this->request->isPost()) {
            $vo['menu_keys'] = implode(',', $vo['menu_keys']);
        }
    }

    /**
     * 一维数组生成数据树
     * @param array $list 待处理数据
     * @param string $cid 自己的主键
     * @param string $pid 上级的主键
     * @param string $sub 子数组名称
     * @return array
     */
    public static function arr2tree(array $list, string $cid = 'id', string $pid = 'pid', string $sub = 'sub'): array
    {
        [$tree, $tmp] = [[], array_combine(array_column($list, $cid), array_values($list))];
        foreach ($list as $vo) isset($vo[$pid]) && isset($tmp[$vo[$pid]]) ? $tmp[$vo[$pid]][$sub][] = &$tmp[$vo[$cid]] : $tree[] = &$tmp[$vo[$cid]];
        unset($tmp, $list);
        return $tree;
    }



}